<?php
class Marketing_XmlController extends Zendvn_Controller_Action {
		
	protected $_arrParam; //Mang tham so nhan duoc khi mot Action chay
	protected $_currentController; //Duong dan cua Controller
	protected $_actionMain; //Duong dan cua Action chinh
	
	public function init(){
		
		$this->_arrParam = $this->_request->getParams(); //Mang tham so nhan duoc khi mot Action chay
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller']; //Duong dan cua Controller
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/'	. $this->_arrParam['controller'] . '/index'; //Duong dan cua Action chinh
	
		//Truyen ra ngoai view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
	
		$this->view->siteConfig = Zend_Registry::get('siteConfig');
		$template_path = TEMPLATE_PATH . "/admin/" . $this->view->siteConfig['template']['admin'];
		$this->loadTemplate($template_path, 'template.ini', 'daotao');
	}
	
	public function indexAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$tblKhachhang 	= new Khachhang_Model_Item();
		$listKhachhang 	= $tblKhachhang->listItem($this->_arrParam, array('task' => 'admin-list'));
		/* echo '<pre>';
		print_r($listKhachhang);
		echo '</pre>'; */
		
		$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/config/1.xml';
		
		$doc = new DOMDocument();
		
		//Tạo
		$doc->version = '1.0';
		$doc->encoding = 'UTF-8';
		$comment_elt = $doc->createComment('Generated by namit@langmaster.edu.vn ');
		$doc->appendChild($comment_elt);
		
		$note_goc = $doc->createElement( 'item' );
		$doc->appendChild($note_goc);
		
		foreach ( $listKhachhang AS $key => $val ) {
			$note_elt = $doc->createElement( 'user' );
			$note_goc->appendChild($note_elt);
			
			$id      	= $doc->createElement( 'id', $val['id'] );
			$name		= $doc->createElement( 'name', $val['name'] );
			$trangthai	= $doc->createElement( 'trangthai', 'Có đi' );
			$price		= $doc->createElement( 'price', 'Miễn phí' );
			
			$note_elt->appendChild($id);
			$note_elt->appendChild($name);
			$note_elt->appendChild($trangthai);
			$note_elt->appendChild($price);
		}
		
		$doc->formatOutput = true;
		
		$doc->saveXML();
		$doc->save($filename);
		
	}
	
	public function listAction() {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/config/1.xml';
		$xml = simplexml_load_file($filename);
		$item = $xml->xpath("//*");
		echo '<pre>';
		print_r($item[0]->name);
		echo '</pre>';
		//$item[0]->name;
		/* $arr = array();
		foreach ($xml AS $val){
			$arr['id'] = $val[0]->id;
		}
		$xml = new Zend_Config_Xml($filename); */
		echo '<br>Danh sách';
		echo '<pre>';
		print_r($xml);
		echo '</pre>';
	}
	
	public function existAction($id = null) {
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		if(!empty($id)){
			$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/config/1.xml';
			$q 	= "//*[id ='" . $id . "']";
			$xml = simplexml_load_file($filename);
				
			$target = $xml->xpath($q);
				
			if(!$target)
				return false;
			else
				return true;
		}
	}
	
	public function addAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
	
		$id = 16;
		if( !$this->existAction($id)){
			$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/config/1.xml';
				
			$doc = new DOMDocument();
				
			$xml = simplexml_load_file($filename);
			
			$employee = $xml->addChild('user');
			$employee->addChild('id', $id);
			$employee->addChild('name', 'Quân XXX');
			$employee->addChild('trangthai', 'Không đi');
			$employee->addChild('price', '1000000');
			
			file_put_contents($filename, $xml->asXML());
		}
	}
	
	public function editAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		if (!empty($this->_arrParam['id'])) {
			$id = $this->_arrParam['id'];
			$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/config/1.xml';
			
			$xml = simplexml_load_file($filename);
			
			$item = $xml->xpath("//*[id ='" . $id . "']");
			$item[0]->name = 'Cảnh';
			$xml->asXML($filename);
		}
	}
	
	public function deleteAction(){
		$this->_helper->layout->disableLayout();
		$this->_helper->viewRenderer->setNoRender();
		
		if (!empty($this->_arrParam['id'])) {
			$id = $this->_arrParam['id'];
			$filename 	= MODULE_PATH . '/'.$this->_arrParam['module'].'/config/1.xml';
			$q 	= "//*[id ='" . $id . "']";
			//Xóa
			//Cách 1
			/* $doc = new DOMDocument();
			$doc->load($filename);
			
			$xpath = new DOMXPath($doc);
			$q = "//*[id ='" . $id . "']";
			foreach ($xpath->query($q) as $node) {
				$businessNode = $node->parentNode;
				$businessNode->removeChild($node);
			}
			$last = $doc->saveXml();
			file_put_contents($filename, $last); */
			
			//Cách 2
			$xml = simplexml_load_file($filename);
			
			$target = $xml->xpath($q);
			
			if(!$target)
				return; //Returns null
			
			$domRef = dom_import_simplexml($target[0]); //Select position 0 in XPath array
			$domRef->parentNode->removeChild($domRef);
			
			$dom = new DOMDocument('1.0');
			$dom->preserveWhiteSpace = false;
			$dom->formatOutput = true;
			$dom->loadXML($xml->asXML());
			$dom->save($filename);
		}
	}
}
